package cn.edu.ctbu.thymeleafwork.service.impl;

import cn.edu.ctbu.thymeleafwork.domain.Student;
import cn.edu.ctbu.thymeleafwork.repository.StudentRepository;
import cn.edu.ctbu.thymeleafwork.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService {

    @Autowired
    private StudentRepository studentRepository;
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }


    /**
     * 分页，带过滤的查询
     * @param student
     * @return
     */

    public Page<Student> findAll(Example<Student> student,Pageable pageable){
        return studentRepository.findAll(student,pageable);
    }
    public Student insert(Student student){
        return studentRepository.save(student);
    }

    public Student update(Student student){
        return studentRepository.save(student);
    }

    public void delete(Student student){
        studentRepository.delete(student);
    }

    public void delete(Integer id){

        Student student=new Student();
        student.setId(id);

        studentRepository.delete(student);
    }

    public Page<Student> findByName(String name, Pageable pageable) {
        return studentRepository.findByName(name, pageable);
    }

    public Student getById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }

    public List<Student> findByNameLike(String name){


        return studentRepository.findByNameLike(name);
    }

}